package com.victualler.core.gen.service.impl;

import com.victualler.core.common.utils.FileUtil;
import com.victualler.core.gen.service.EntityModelService;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;



/**
 * 五层类生成服务
 * @author guoshichang
 */
@Service
public class EntityModelServiceImpl implements EntityModelService {

	/**
	 * 根据参数生成五层结构
	 * @param modelName 子maven项目名称 没有则填写无
	 * @param basePackage 基础模块包名
	 * @param entityPreName entity实体的前部分(不含DTO)
	 */
	@Override
	public void genEntityFiveLayer(String modelName,String basePackage,String entityPreName){
			try {
				if(modelName!=null && modelName.trim().length()>0){
					modelName+="/";
				}
				//repository层
				this.genEntityRepository(modelName, basePackage, entityPreName);
				this.genEntityQueryRepository(modelName, basePackage, entityPreName);
				this.genEntityQueryRepositoryImpl(modelName, basePackage, entityPreName);
				//service层
				this.genEntityService(modelName, basePackage, entityPreName);
				this.genEntityServiceImpl(modelName, basePackage, entityPreName);
				//controller层
				this.genEntityContoller(modelName, basePackage, entityPreName);
				this.genEntityContollerImpl(modelName, basePackage, entityPreName);
				//初始化页面
				this.genListPage(modelName, basePackage, entityPreName);
				this.genEditPage(modelName, basePackage, entityPreName);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}



	private void genListPage(String modelName,String basePackage,String entityPreName) throws IOException{
		String content = this.getModelFileContent("/model/page/ListPage");
		content = content.replaceAll("\\$\\{entityPreName}", entityPreName);
		String baseDir = System.getProperty("user.dir")+"/"+modelName+"src/main/java/"+basePackage.replaceAll("\\.", "/")+"/page/";
		String fileName  =entityPreName+"List.html";
		//生成文件
		FileUtil.printToFile(content, baseDir, fileName, "UTF-8");
	}

	private void genEditPage(String modelName,String basePackage,String entityPreName) throws IOException{
		String content = this.getModelFileContent("/model/page/EditPage");
		content = content.replaceAll("\\$\\{entityPreName}", entityPreName);
		String baseDir = System.getProperty("user.dir")+"/"+modelName+"src/main/java/"+basePackage.replaceAll("\\.", "/")+"/page/";
		String fileName  =entityPreName+"Edit.html";
		//生成文件
		FileUtil.printToFile(content, baseDir, fileName, "UTF-8");
	}
	
	private void genEntityContoller(String modelName,String basePackage,String entityPreName) throws IOException{
		 String content = this.getModelFileContent("/model/controller/EntityController");
		 content = content.replaceAll("\\$\\{basePackage}", basePackage);
		 content = content.replaceAll("\\$\\{entityPreName}", entityPreName);
		String baseDir = System.getProperty("user.dir")+"/"+modelName+"src/main/java/"+basePackage.replaceAll("\\.", "/")+"/controller/";
		String fileName  =entityPreName+"Controller.java";
		//生成文件
		FileUtil.printToFile(content, baseDir, fileName, "UTF-8");
	}
	
	private void genEntityContollerImpl(String modelName,String basePackage,String entityPreName) throws IOException{
		 String content = this.getModelFileContent("/model/controller/EntityControllerImpl");
		 content = content.replaceAll("\\$\\{basePackage}", basePackage);
		 content = content.replaceAll("\\$\\{entityPreName}", entityPreName);
		 String urlName = basePackage.replace("com.victualler.", "/").replaceAll("\\.", "/");
		 content = content.replaceAll("\\$\\{classMapUrl}", urlName);
		String baseDir = System.getProperty("user.dir")+"/"+modelName+"src/main/java/"+basePackage.replaceAll("\\.", "/")+"/controller/impl/";
		String fileName  =entityPreName+"ControllerImpl.java";
		//生成文件
		FileUtil.printToFile(content, baseDir, fileName, "UTF-8");
	}
	
	
	private void genEntityService(String modelName,String basePackage,String entityPreName) throws IOException{
		 String content = this.getModelFileContent("/model/service/EntityService"); 
		 content = content.replaceAll("\\$\\{basePackage}", basePackage);
		 content = content.replaceAll("\\$\\{entityPreName}", entityPreName);
		String baseDir = System.getProperty("user.dir")+"/"+modelName+"src/main/java/"+basePackage.replaceAll("\\.", "/")+"/service/";
		String fileName  =entityPreName+"Service.java";
		//生成文件
		FileUtil.printToFile(content, baseDir, fileName, "UTF-8");
	}
	
	private void genEntityServiceImpl(String modelName,String basePackage,String entityPreName) throws IOException{
		 String content = this.getModelFileContent("/model/service/EntityServiceImpl"); 
		 content = content.replaceAll("\\$\\{basePackage}", basePackage);
		 content = content.replaceAll("\\$\\{entityPreName}", entityPreName);
		String baseDir = System.getProperty("user.dir")+"/"+modelName+"src/main/java/"+basePackage.replaceAll("\\.", "/")+"/service/impl/";
		String fileName  =entityPreName+"ServiceImpl.java";
		//生成文件
		FileUtil.printToFile(content, baseDir, fileName, "UTF-8");
	}
	
	
	
	private void genEntityQueryRepositoryImpl(String modelName,String basePackage,String entityPreName) throws IOException{
		 String content = this.getModelFileContent("/model/repository/EntityQueryRepositoryImpl"); 
		 content = content.replaceAll("\\$\\{basePackage}", basePackage);
		 content = content.replaceAll("\\$\\{entityPreName}", entityPreName);
		String baseDir = System.getProperty("user.dir")+"/"+modelName+"src/main/java/"+basePackage.replaceAll("\\.", "/")+"/repository/impl/";
		String fileName  =entityPreName+"QueryRepositoryImpl.java";
		//生成文件
		FileUtil.printToFile(content, baseDir, fileName, "UTF-8");
	}
	
	
	private void genEntityRepository(String modelName,String basePackage,String entityPreName) throws IOException{
		 String content = this.getModelFileContent("/model/repository/EntityRepository"); 
		 content = content.replaceAll("\\$\\{basePackage}", basePackage);
		 content = content.replaceAll("\\$\\{entityPreName}", entityPreName);
		String baseDir = System.getProperty("user.dir")+"/"+modelName+"src/main/java/"+basePackage.replaceAll("\\.", "/")+"/repository/";
		String fileName  =entityPreName+"Repository.java";
		//生成文件
		FileUtil.printToFile(content, baseDir, fileName, "UTF-8");
	}
	
	private void genEntityQueryRepository(String modelName,String basePackage,String entityPreName) throws IOException{
		 String content = this.getModelFileContent("/model/repository/EntityQueryRepository"); 
		 content = content.replaceAll("\\$\\{basePackage}", basePackage);
		 content = content.replaceAll("\\$\\{entityPreName}", entityPreName);
		String baseDir = System.getProperty("user.dir")+"/"+modelName+"src/main/java/"+basePackage.replaceAll("\\.", "/")+"/repository/";
		String fileName  =entityPreName+"QueryRepository.java";
		//生成文件
		FileUtil.printToFile(content, baseDir, fileName, "UTF-8");
	}
	
	
	
	
	private String getModelFileContent(String path) throws IOException{
		 BufferedReader reader = new BufferedReader( new InputStreamReader(this.getClass().getResourceAsStream(path)));
		 StringBuffer result = new StringBuffer();
		 String line = null;
        while ((line = reader.readLine()) != null) {
       	 result.append(line+"\r\n");
        }
        return result.toString();
	}
}
